#define _CRT_SECURE_NO_WARNINGS 1
#include"Seqlist.h"
//void SLInit(SL* psl)
//{
//	psl->a = NULL;
//	psl->capacity = 0;
//	psl->size = 0;
//}
void SLInit(SL* psl)
{
	psl->a = (SLDatatype*)malloc(sizeof(SLDatatype) * 4);
	if (psl->a == NULL)
	{
		perror("malloc fail");
		return;
	}
	psl->capacity = 4;
	psl->size = 0;
}
void SLDestroy(SL* psl)
{
	free(psl->a);
	psl->a = NULL;
	psl->capacity = psl->size = 0;
}

void SLCheckCapacity(SL* psl)
{
	assert(psl);
	if (psl->capacity == psl->size)
	{
		SLDatatype* tmp = (SLDatatype*)realloc(psl->a, sizeof(SLDatatype) * 2 * psl->capacity);
		if (tmp == NULL)
		{
			perror("realloc fail");
			return;
		}
		psl->a = tmp;
		psl->capacity *= 2;
	}
}

void SLPrint(SL* psl)
{
	int i = 0;
	for (i = 0; i < psl->size; i++)
	{
		printf("%d ", psl->a[i]);
	}
	printf("\n");
}

void SLPushBack(SL* psl, SLDatatype x)
{
	SLCheckCapacity(psl);
	psl->a[psl->size] = x;
	psl->size++;
}
void SLPushFront(SL* psl, SLDatatype x)
{
	assert(psl->size);
	SLCheckCapacity(psl);
	int end = psl->size - 1;
	while (end >= 0)
	{
		psl->a[end + 1] = psl->a[end];
		end--;
	}
	psl->a[0] = x;
	psl->size++;
}
void SLPopBack(SL* psl)
{
	assert(psl->size > 0);
	psl->size--;
}
void SLPopFront(SL* psl)
{
	assert(psl->size > 0);
	int start = 0;
	while (start < psl->size - 1)
	{
		psl->a[start] = psl->a[start + 1];
		start++;
	}
	psl->size--;
}

void SLInsert(SL* psl, int pos, SLDatatype x)
{
	assert(pos >= 0 && pos <= psl->size);
	SLCheckCapacity(psl);
	int cur = psl->size - 1;
	while (pos <= cur)
	{
		psl->a[cur + 1] = psl->a[cur];
		cur--;
	}
	psl->a[pos] = x;
	psl->size++;
}
void SLErase(SL* psl, int  pos)
{
	assert(pos >= 0 && pos < psl->size);
	int end = pos + 1;
	while (pos <= psl->size)
	{
		psl->a[pos] = psl->a[pos + 1];
		pos++;
	}
	psl->size--;
}

int SLFind(SL* psl, SLDatatype x)
{
	assert(psl);
	for (int i = 0; i < psl->size; i++)
	{
		if (psl->a[i] == x)
			return i;
	}
	return -1;
}
void Modify(SL* psl, int pos, SLDatatype x)
{
	assert(psl);
	psl->a[pos] = x;
}